package com.ztman.cusview.customView;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Created by YJH on 2017/12/12.
 */

public class LoveGroup extends ViewGroup {
    private Context mContext;
    ArrayList<ArrayList<View>> mViewLines;
    private int iWidthSpace, iHeightSpace;//ViewGroup 的宽高尺寸

    public LoveGroup(Context context) {
        this(context, null);
    }

    public LoveGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoveGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        init();

    }

    private void init() {

    }

    /**
     * 功能：由于自定义view的子View都会带 margin ，因此重新这个方法就能获得 margin 的值，便于计算。
     *
     * @param attrs
     * @return
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return super.generateLayoutParams(attrs);
    }


    private int iWithSize;

    /**
     * 功能：自定义view的三步走
     * 第一步：测量
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mViewLines = new ArrayList<>();
        //获取 spec 模式
        int iWithMode = MeasureSpec.getMode(widthMeasureSpec);
        int iHeightMode = MeasureSpec.getMode(heightMeasureSpec);

        //获取 spec 尺寸
        iWithSize = MeasureSpec.getSize(widthMeasureSpec);
        int iHeightSize = MeasureSpec.getSize(heightMeasureSpec);

        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int measureWith = 0;
        int measureHeight = 0;

        if (iWithMode == MeasureSpec.EXACTLY && iHeightMode == MeasureSpec.EXACTLY) {

            measureWith = iWithSize;
            measureHeight = iHeightSize;
        } else {

            int curLineW = 0;// 当前行所有的View所占据的水平空间宽度
            int curLineH = 0;// 当前行的View所占据的竖直空间高度
            int curMaxW = 0;//存储当前所有行最宽
            ArrayList<View> lineViews = new ArrayList<>();//一行封装了多少的view
            final int childCount = getChildCount();
            Log.e("打印==>>", "输出子view的个数$$" + childCount);
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);

//                //通过方法 generateLayoutParams的重写，实现对子View的 margin值获取。
                MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
//                //从而获取子View 的实际宽、高的空间值
//                int widthSpace = child.getMeasuredWidth() + params.rightMargin + params.leftMargin;
//                int heightSpace = child.getMeasuredHeight() + params.rightMargin + params.leftMargin;

                int widthSpace = child.getMeasuredWidth();
                int heightSpace = child.getMeasuredHeight();

                /**
                 * 如果 当前行所有的View所占据的水平空间宽度 + 尝试放置一个新的View宽度 > ViewGroup 自身的宽度
                 * 这样，就要换行了
                 */
                if (curLineW + widthSpace > iWithSize) {

                    //记录下一行所处的行高 view.top
                    curLineH += heightSpace;
                    curMaxW = Math.max(curMaxW, curLineW);//存储当前所有行最宽
                    curLineW = 0;//换行归0
                    mViewLines.add(lineViews);//行-容器 加入了一行view

                    lineViews = new ArrayList<>();
                    lineViews.add(child);
                    curLineW += widthSpace;

                } else {
                    //放入一个新的view，记录下目前当前行所有view的所占据的水平空间值
                    curLineW += widthSpace;
                    lineViews.add(child);//view-容器 加入了一个view
                }


            }

            measureHeight = curLineH;
            measureWith = curMaxW;

        }


        //measureWith这里有一个bug measureWith必须要传入所有行中最宽的那个值
        //保存自己尺寸
        Log.e("打印==>>", "measureWith=" + measureWith + "measureHeight=" + measureHeight);
        setMeasuredDimension(measureWith, measureHeight);

    }

    /**
     * 功能：自定义view的三步走
     * 第二步：归位（安置排放）
     *
     * @param b
     * @param i
     * @param i1
     * @param i2
     * @param i3
     */
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        Log.e("打印==>>", "输出子view的个数==" + mViewLines.size());
        Log.e("打印==>>", "输出子view的个数**" + getChildCount());
        /**
         * 目前进行到这里，已经给view分过行了，每行有多少的view，已经封装到mViewLines中了。
         * 而现在要做的就是给每一行，每行的每个view放置。
         */

        final int lineSize = mViewLines.size();
        Log.e("打印==>>", "输出子view的行==>" + lineSize);
        for (int j = 0; j < lineSize; j++) {
            ArrayList<View> lineViews = mViewLines.get(j);//获取有多少行封装了view的容器
            Log.e("打印==>>", "输出子view的行中的view数==>" + lineViews.size());
            int iLineViewWidth = 0;//当前行中已经存在的View，他们在水平方向所占据的空间
            int iLineViewHeight = 0;//当前行中已经存在的View，他们在竖直方向所占据的空间


            for (int k = 0; k < lineViews.size(); k++) {
                View childView = lineViews.get(k);//获取每行的view
//                //通过方法 generateLayoutParams的重写，实现对子View的 margin值获取。
//                MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
//                //从而获取子View 的实际宽、高的空间值
//                int widthSpace = childView.getMeasuredWidth() + params.rightMargin + params.leftMargin;
//                int heightSpace = childView.getMeasuredHeight() + params.topMargin + params.bottomMargin;

                int widthSpace = childView.getMeasuredWidth();
                int heightSpace = childView.getMeasuredHeight();

                iLineViewWidth += widthSpace;
                iLineViewHeight = (j + 1) * heightSpace;
                int curLeft = iLineViewWidth - widthSpace;
                int curRight = iLineViewWidth;
                int curTop = iLineViewHeight - heightSpace;
                int curBottom = iLineViewHeight;

//                int curLeft = iLineViewWidth - (childView.getMeasuredWidth() + params.rightMargin);
//                int curRight = iLineViewWidth;
//                int curTop = iLineViewHeight - (childView.getMeasuredHeight() + params.bottomMargin);
//                int curBottom = iLineViewHeight;


                Log.e("打印==>>j=" + j, "curLeft=" + curLeft + "curTop=" + curTop + "curRight=" + curRight + "curBottom=" + curBottom);
                childView.layout(curLeft, curTop, curRight, curBottom);
            }


        }
    }

    /**
     * 功能：自定义view的三步走
     * 第三步：绘制
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

}
